<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 自定义call */
    Function.prototype.call = function (thisObj, ...args) {
      console.log('call')
      
      // return this(...args)   // 问题: this不对

      // 处理thisObj为undefined和null的情况
      if (thisObj===undefined || thisObj===null) {
        return this(...args) 
        // thisObj = window
      }


      // 将当前函数(也就是this)添加为thisObj的临时方法
      thisObj.tempFn = this

      // 通过thisObj来调用这个方法  => 也就是执行当前函数this
      const result  = thisObj.tempFn(...args)

      // 删除临时方法
      delete thisObj.tempFn

      // 返回方法执行的结果
      return result
    }
    // 对象快照: 打印时对象的样子
  </script>

  <script>
    function fn(a, b) {
      console.log(this, a, b)
      return a + b
    }
    const obj = {m: 1}
    console.log(fn.call(obj, 2, 3))
    console.log(fn.call(null, 2, 3))

  </script>
</body>
</html>